<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:include  page="../common/file.jsp"/>
<head>
</head>
<body class="dialog_body">
<form id="submit_form" action="${pageContext.request.contextPath}/role/grant" method="post" target="_parent">
<input type="hidden" id="roleId" name="roleId" value="${roleId }"/>
<div class="query-detail-div" style="display:block">
	<div class="left-tree-div" style="width:200px;float:left;margin-top:15px;margin-left:40px;border:0;height:380px;background-color:rgba(0, 0, 0, 0.3); color:#fff">
		<div class="left-tree-div-top" style="width:200px;height:30px;line-height:30px;font-weight:bold;background-color:rgba(0, 0, 0, 0.4);letter-spacing:2px;text-indent:10px;color:#fff">系统资源</div>
		<div class="left-tree-div-bottom" style="overflow:auto;height:349px">
			<form id="roleGrantForm" method="post">
	            <input name="id" type="hidden"  value="${roleId}" readonly="readonly">
	            <ul id="resourceTree"></ul>
	            <input id="menuIds" name="menuIds" type="hidden" />
	        </form>
        </div>
	</div>
	<div class="right-tree-div" style="width:95px;margin-top:15px;margin-left:40px;height:380px;display:inline-block">
		 <button style="margin-bottom:20px" type="button" class="btn btn-red" onclick="checkAll();">全 选</button>
<!--          <button style="margin-bottom:20px" type="button" class="btn btn-red" onclick="checkInverse();">反 选</button> -->
         <button type="button" class="btn btn-red" onclick="uncheckAll();">取 消</button>
	</div>
	<div style="display:block;clear:both;margin-top:20px;text-align:center">
		 <button type="button" class="btn btn-warning submit-btn">提 交</button>
		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <button type="button" class="btn btn-default cancel-btn" target="_self">返 回</button>
	</div>
</div>
</form>
<script type="text/javascript">
	//初始化滚动条
	$(".left-tree-div-bottom").niceScroll({cursorcolor:"#000000",zindex:999999,bouncescroll:true,cursoropacitymax:0.4,cursorborder:"",cursorborderradius:7,cursorwidth:"7px",background:"rgba(0,0,0,.1)",autohidemode:false,railpadding:{top:0,right:2,left:2,bottom:0}});
	
	$.stringToList = function(value) {
	    if (value != undefined && value != '') {
	        var values = [];
	        var t = value.split(',');
	        for ( var i = 0; i < t.length; i++) {
	            values.push('' + t[i]);/* 避免他将ID当成数字 */
	        }
	        return values;
	    } else {
	        return [];
	    }
	};
    var resourceTree;
    $(function() {
    	var ctxt = "${pageContext.request.contextPath}";
        resourceTree = $('#resourceTree').tree({
            url : ctxt+'/menu/findAllTrees',
            parentField : 'pid',
            lines : true,
            checkbox : true,
            cascadeCheck : false,
            onClick : function(node) {
            },
            onCheck:function(node, checked) {
            	if (checked) { // 选中（所有父节点选中）
            		var parentNode = resourceTree.tree('getParent', node.target);
                	if (parentNode != null && parentNode != 'undefined') {
                		resourceTree.tree('check', parentNode.target);
                	}
            	} else { //取消选中（所有子节点取消选中）
            		var childNode = resourceTree.tree('getChildren', node.target);
           			if (childNode.length > 0) {
                    	for (var i = 0; i < childNode.length; i++) {
                        	resourceTree.tree('uncheck', childNode[i].target);
                    	}
           		  	}
            	}
            	
            },
            onLoadSuccess : function(node, data) {
                $.post( ctxt+'/roleMenu/list', {
                    roleId : '${roleId }'
                }, function(result) {
                    var ids;
                    if (result != undefined && result.status == "1") {
                        ids = $.stringToList(result.data + '');
                    }
                    if (ids.length > 0) {
                        for ( var i = 0; i < ids.length; i++) {
                            if (resourceTree.tree('find', ids[i])) {
                                resourceTree.tree('check', resourceTree.tree('find', ids[i]).target);
                            }
                        }
                    }
                }, 'json');
            }
            
        });
        
    });
    
	//全选
    function checkAll() {
        var nodes = resourceTree.tree('getChecked', 'unchecked');
        if (nodes && nodes.length > 0) {
            for ( var i = 0; i < nodes.length; i++) {
                resourceTree.tree('check', nodes[i].target);
            }
        }
    }
  	//取消
    function uncheckAll() {
        var nodes = resourceTree.tree('getChecked');
        if (nodes && nodes.length > 0) {
            for ( var i = 0; i < nodes.length; i++) {
                resourceTree.tree('uncheck', nodes[i].target);
            }
        }
    }
  	//反选
    function checkInverse() {
        var unchecknodes = resourceTree.tree('getChecked', 'unchecked');
        var checknodes = resourceTree.tree('getChecked');
        if (unchecknodes && unchecknodes.length > 0) {
            for ( var i = 0; i < unchecknodes.length; i++) {
                resourceTree.tree('check', unchecknodes[i].target);
            }
        }
        if (checknodes && checknodes.length > 0) {
            for ( var i = 0; i < checknodes.length; i++) {
                resourceTree.tree('uncheck', checknodes[i].target);
            }
        }
    }
  	
  	//提交保存
  	$('.submit-btn').click(function() {
  		var checknodes = resourceTree.tree('getChecked');
        var ids = [];
        if (checknodes && checknodes.length > 0) {
            for ( var i = 0; i < checknodes.length; i++) {
                ids.push(checknodes[i].id);
            }
        }
        $(".submit-btn").unbind("click");
        $('#menuIds').val(ids);
		$('#submit_form').submit();
	    return false;
	});
    
    //关闭窗口
	$('.cancel-btn').click(function() {
		art.dialog.close();
	});
</script>
</body>

  
